<?php
/**
 * CRMLite Default Admin Controller
 *
 * @final
 * @package Crmlite_App
 * @version $Id: AdminBaseController.php 55 2007-09-09 12:44:28Z etng2004 $
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */

abstract class AdminBaseController extends Crmlite_Controller_Action
{
	public function init()
	{
		parent::init();
		$this->initData();
		if($this->S_staff = Zend_Auth::getInstance()->getStorage()->read())
		{
			$this->S_staff_id = $this->S_staff['id'];
		}
		$this->path['root']['link'] = $this->module_url;
	}
    /**
     * 删除缓存
     *
     * 如果不指定记录ID，则默认删除所有相应的缓存
     *
     * @param int $id
     * @todo 实现删除所有的功能
     */
	protected function removeCache( $id = null )
	{
		$Cache = Zend_Registry::get('cache');
		/* @var $Cache Zend_Cache_Core */
		if($id && $this->model)
		{
			$cache_id = $this->model->dbt->getTable() . $id;
			$Cache->remove($cache_id);
		}
	}
	protected function initData()
	{
		$cache_id = 'client_types';
		if(!($this->client_types = $this->cache->get($cache_id)))
		{
			$ClientType = new ClientTypeModel();
			$this->client_types = $ClientType->asPairs('code', 'name');
			$this->cache->save($this->client_types, $cache_id);
		}
	  	$cache_id = 'trade_types';
		if(!($this->trade_types = $this->cache->get($cache_id)))
		{
			$TradeType = new TradeTypeModel();
			$this->trade_types = $TradeType->asPairs('id', 'name');
			$this->cache->save($this->trade_types, $cache_id);
		}
	}

	/**
	 * 在进行分发前检测是否是没有登录的情况
	 * 1、没登陆则需要登录
	 * 2、如果还没有管理员则提醒创建第一个管理员
	 *
	 *
	 */
    public function preDispatch()
    {
        $this->log($this->getHistory());
    	if($this->S_staff)return true;

    	$controller = $this->getRequest()->getControllerName();
    	$action = $this->getRequest()->getActionName();
    	$SM = new StaffModel();
    	$white_list = array(
    	'index' => array('login', 'captcha', 'logout', 'message'),
    	);
    	$pass = false;
    	if($staff_count = $SM->count())
    	{
	    	foreach ($white_list as $white_controller => $white_actions)
	    	{
	    		if($controller == $white_controller && in_array($action, $white_actions))
	    		{
	    			$pass = true;
	    			break;
	    		}
	    	}
    	}
    	else
    	{
    		/**
    		 * 系统无管理员，畅通无阻
    		 */
    		$pass = true;
    		$this->S_NO_STAFF = true;
    	}
    	/**
    	 * 此处
    	 */
    	if(!$pass)
    	{
    		$this->_forward('login', 'index');
    		$this->view->message = "Login First!";
    	}
		return true;
    }

	public function postDispatch()
	{
		parent::postDispatch();
		$this->view->page		= $this->page;
		$this->view->path		= $this->path;
		$this->view->S_staff 	= $this->S_staff;
		$this->view->client_types = $this->client_types;
		$this->view->trade_types = $this->trade_types;
	}
	public function isLogin()
    {
        return !is_null($this->S_staff);
    }
    /**
     * testPriv
     *
     * @param string $res
     * @param string $priv
     * @param boolean $extra
     * @access public
     * @return void
     */
    public function testPriv($priv = 'ACCESS', $extra = true)
    {
        $result = $this->isAllowed(null, $priv, $extra);
        if (!$result)
        {
            $this->feedback('无权访问！', '注意', $this->module_url , 'warning');
        }
        return $result;
    }
    public function feedback($messgae, $title, $backUrl = '/', $icon = 'note')
    {
        $param = array(
            'message'   =>  $messgae,
            'title'     =>  $title,
            'url'       =>  $backUrl,
            'icon'      =>  $icon,
        );
        $request = $this->getRequest();
        $module  = $request->getModuleName();
        $this->_forward('message', 'index', $module, $param);
    }
    /**
     * isAllowed
     *
     * @param string $res
     * @param string $priv
     * @param boolean $extra
     * @access public
     * @return void
     */
    public function isAllowed($res = null, $priv = null, $extra = true)
    {
        if (is_null($res))
        {
            $request = $this->getRequest();
            $res = $request->getModuleName();
            $res .= '::' . $request->getControllerName();
            $res .= '::' . $this->getRequest()->getActionName();
        }
        $res = is_null($res)? null : strtoupper($res);
        $priv = is_null($priv)? null : strtoupper($priv);
        $acl = new Zend_Acl();
        $result = $acl->isAllowed($this->S_staff_id , $res, $priv);
        return $extra && $result;
    }

}